/**
 * @Description MyPwdController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2017年12月7日 上午9:55:30
 */
package com.solution.pc.my.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.auth.entity.UserEntity;
import com.solution.admin.auth.service.UserService;
import com.solution.admin.customer.entity.CustomerEntity;
import com.solution.admin.customer.service.CustomerService;
import com.solution.admin.home.service.RedisService;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.utils.RandomUtils;
import com.solution.common.utils.ip.IPUtils;
import com.solution.common.utils.sms.AliyunSmsUtils;
import com.solution.common.utils.string.StringUtil;
import com.solution.pc.home.constants.HomeConstants.SmsTemplateType;
import com.solution.wx.my.constants.MyConstants;

/**
 * 我的积分
 * 
 * @author xu_cc
 * @date 创建时间：2017年12月4日 下午7:34:28
 */
@RequestMapping("/pc/my")
@Controller("myPwdController")
public class MyPwdController extends BaseController {

	@Autowired
	UserService userService;

	@Autowired
	CustomerService customerService;

	@Autowired
	RedisService redisService;

	/**
	 * 跳转到修改页面
	 * 
	 * @param request
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月4日 上午11:31:31
	 */
	@RequestMapping("/mypwd")
	public String toMyPwdPage(HttpServletRequest request, Model model) {
		try {
			String mobile = "";
			// 先获取用户信息
			UserEntity user = userService.getUserById(getUserId());
			if (null != user && StringUtil.isNotNull(user.getMobile())) {
				mobile = user.getMobile();
			} else {
				// 从客户信息获取
				CustomerEntity customer = getCustomer();
				if (null != customer && StringUtil.isNotNull(customer.getMobile())) {
					mobile = customer.getMobile();
				}
			}
			String mobileView = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
			model.addAttribute("mobile", mobile);
			model.addAttribute("mobileView", mobileView);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("page", "modPwd");
		model.addAttribute("step", 1);
		return "pc/my/modpwd/my_pwd";
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月4日 上午11:31:31
	 */
	@RequestMapping("/modpwd")
	public String toModPwdPage(HttpServletRequest request, Model model, String mobile, String password) {
		try {
			// 先获取用户信息
			UserEntity user = userService.getUserById(getUserId());
			if (null == user) {
				// 去登录
				return "redirect:/home/loginPage";
			}
			try {
				// 修改客户密码（成功是否不影响最终结果）
				int count = customerService.updateCustomerPassword(mobile, password);
				System.out.println("修改客户密码结果：" + ((count > 0) ? "成功" : "失败"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 修改用户密码
			int rows = userService.resetUserPwdByMobile(user.getId(), user.getUserCode(), password, user.getPasswordSalt());
			System.out.println("修改用户密码结果：" + ((rows > 0) ? "成功" : "失败"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("page", "modPwd");
		model.addAttribute("step", 3);
		return "pc/my/modpwd/my_pwd";
	}

	/**
	 * 校验短信验证码
	 * 
	 * @param request
	 * @param mobileCode
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月6日 下午5:14:21
	 */
	@RequestMapping("/checkMobileCode")
	@ResponseBody
	public BaseReturn checkMobileCode(HttpServletRequest request, String mobileCode) {
		BaseReturn result = new BaseReturn();
		try {
			String verifyCodeKey = MyConstants.MODPWD_MOBILE_KEY.concat(IPUtils.getIntenetIP(request));
			// 手机验证码
			// String authKey = UserDataKey.CODE_MOBILE_CODE +
			// IPUtils.getIntenetIP(request);
			String redisMobileCode = redisService.get(verifyCodeKey);
			// String.valueOf(request.getSession().getAttribute(UserDataKey.CODE_MOBILE_CODE))
			if (mobileCode.equals(redisMobileCode)) {
				result.setCode(SysCode.SUCCESS);
				result.setMsg("短信验证码校验成功~");
				redisService.del(verifyCodeKey);
			} else {
				result.setCode(SysCode.FAILED);
				result.setMsg("短信验证码校验失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("短信验证码校验失败，请重试！");
		}
		return result;
	}

	/**
	 * 发送手机验证码
	 * 
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月16日 上午11:28:41
	 */
	@RequestMapping("/sendMobileCode")
	@ResponseBody
	public BaseReturn sendVerifyCode(HttpServletRequest request, String mobile) {
		BaseReturn result = new BaseReturn();
		try {
			// redis中的验证码KEY
			String verifyCodeKey = MyConstants.MODPWD_MOBILE_KEY.concat(IPUtils.getIntenetIP(request));
			// 发送短信
//			String templateId = "SMS_126866486";
			String verifyCode = RandomUtils.getRandNum(6);
			// 保存到redis(30分钟之内有效)
			redisService.save(verifyCodeKey, verifyCode, (30 * 60));
			System.out.println("============>>>短信验证码:" + verifyCode);
			String params = "{\"code\":\"" + verifyCode + "\"}";
			AliyunSmsUtils.sendSms(SmsTemplateType.SMS_MOD_PWD.getTemplateId(), mobile, params);
			result.setCode(SysCode.SUCCESS);
			result.setMsg("短信验证码发送成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("短信验证码发送失败！");
		}
		return result;
	}
}
